package com.ym.excel.domain;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.xssf.usermodel.XSSFChart;

import java.util.List;

/**
 * @Author: smuswc
 * @Date: 2018/2/5 10:20
 * @Description: 折线图
 */
public class LineChart {

    /**
     * 生成Excel 的同时生成数据
     * @param sheet
     * @param chartTitle
     * @param legends
     * @param axisModel
     * @param dataModel
     */
    public static void drawLineChar(Sheet sheet, String chartTitle, List<String> legends, LineCharModel axisModel, LineCharModel ... dataModel) {

    }


    /**
     * 根据自定义数据绘图到
     * @param axisString
     * @param data
     */
    public static void drawLineChar(Sheet sheet, String chartTitle, List<String> legends, List<String> axisString, List<Number> ... data) throws Exception{
        // 数据校验
        check(sheet, legends, axisString, data);

        // 创建并获取Draw对象
        Drawing drawing = sheet.createDrawingPatriarch();
        // 指定绘制区域
        ClientAnchor clientAnchor = drawing.createAnchor(0, 0, 26, 70, 1, 2, 8, 20);
        clientAnchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
        // 创建图表信息
        Chart chart = drawing.createChart(clientAnchor);

        LineChartData lineChartData = chart.getChartDataFactory().createLineChartData();
        // 创建数据图标工厂
        ChartAxisFactory factory = chart.getChartAxisFactory();
        // 创建类别轴信息
        ChartAxis categoryAxis = factory.createCategoryAxis(AxisPosition.BOTTOM);
        categoryAxis.setCrosses(AxisCrosses.AUTO_ZERO);
        // 创建值轴信息
        ChartAxis valueAxis = factory.createValueAxis(AxisPosition.RIGHT);
        valueAxis.setCrosses(AxisCrosses.AUTO_ZERO);
        ChartDataSource<String> xAxis = DataSources.fromArray((String[])axisString.toArray());
        for (int i = 0, length = data.length; i < length; i++) {
            ChartDataSource<Number> dataAxis = DataSources.fromArray((Number[])data[i].toArray());
            // 设置数据和横轴信息
            LineChartSeries lineChartSeries = lineChartData.addSeries(xAxis, dataAxis);
            // 设置图例信息
            lineChartSeries.setTitle(legends.get(i));
        }

        // 创建或获取图例操作信息
        ChartLegend chartLegend = chart.getOrCreateLegend();
        // 设置图例位置
        chartLegend.setPosition(LegendPosition.TOP_RIGHT);

        // 设置图表title
        chart.plot(lineChartData, categoryAxis, valueAxis);
        ((XSSFChart) chart).setTitleText(chartTitle);
    }

    /**
     * 数据信息校验
     * @param sheet
     * @param legends
     * @param axisString
     * @param data
     * @throws Exception
     */
    private static void check(Sheet sheet, List<String> legends, List<String> axisString, List<Number>... data) throws Exception{
        if (sheet == null) {
            throw new Exception("Sheet 不能为空！");
        }
        if (data == null || data.length<=0) {
            throw new Exception("绘制数据不能为空！");
        }
        if (legends == null || legends.size() != data.length) {
            throw new Exception("图例为空或与数据项不符！");
        }
        int size = axisString.size();
        for (List<Number> list: data) {
            if (list.size() != size)
                throw new Exception("数据与刻度数据信息不付！");
        }
    }

    /**
     *
     */
    public class LineCharModel {
        int row;
        int startColumn;
        int entColumn;

        public int getRow() {
            return row;
        }

        public void setRow(int row) {
            this.row = row;
        }

        public int getStartColumn() {
            return startColumn;
        }

        public void setStartColumn(int startColumn) {
            this.startColumn = startColumn;
        }

        public int getEntColumn() {
            return entColumn;
        }

        public void setEntColumn(int entColumn) {
            this.entColumn = entColumn;
        }
    }
}
